package com.st.rzs.excel.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;

import com.st.rzs.excel.vo.FiledDescription;
import com.st.rzs.excel.vo.SheetDesc;

/**
 * 数据导出成excel,response相应输出
 * 
 * @author xieyang
 * 
 */
public class ExcelUtilTest {

	public static void main(String[] args) {

	}

	/**
	 * 拼接待处理模型对象，各个模块自行负责，sheet描述及内容用户自行书写，单个sheet内容行数不能超过65533
	 * 
	 * @return
	 * @throws Exception
	 */
	private void exportExcel() throws Exception {

		// 设置excel列名称与后台实体属性名称的关系，查询数据是基于反射做的，name就是返回模型的字段名称
		List<FiledDescription> descriptions = new ArrayList<FiledDescription>();
		descriptions.add(ExcelUtil.createTitle("姓名", "name"));
		descriptions.add(ExcelUtil.createTitle("性别", "sex"));
		descriptions.add(ExcelUtil.createTitle("年龄", "age"));
		descriptions.add(ExcelUtil.createTitle("地址", "address"));
		// 创建sheet描述
		SheetDesc desc = ExcelUtil.createSheetDesc("测试标题1", "测试Sheet名称1", descriptions);

		// 拼接待显示的内容
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i <= 1000000; i++) {
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("name", "谢阳" + i);
			map1.put("sex", "男" + i);
			map1.put("age", "2" + i);
			map1.put("address", "陕西省西安市" + i);
			list.add(map1);
		}
		Workbook workbook = ExcelUtil.createWorkbook(desc, list, true);
		String fileName = FileUtil.getExportFileName("一键搜日志");
		// 此对象用户各个封装并返回，单个sheet内容默认不能超过65533,多处部分,导出时候程序自行截断
		HttpServletResponse response = null;
		ExcelUtil.writeExcelToResponse(response, fileName, workbook);

	}

}